package fpt.hungnp6.boxlauncher;

import java.util.List;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 
public class MyAdapter extends BaseAdapter {
 
    private Context myContext;
    private List<ResolveInfo> MyAppList;
    PackageManager myPackageManager;
 
    MyAdapter(Context c, List<ResolveInfo> appIntentList) {
        myContext = c;
        MyAppList = appIntentList;
        myPackageManager = c.getPackageManager();
    }
 
    @Override
    public int getCount() {
        return MyAppList.size();
    }
 
    @Override
    public Object getItem(int position) {
        return MyAppList.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
    static class ViewHolder{
		TextView textView;
		ImageView imageView;
	}
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
 
        ViewHolder viewHolder;
        LayoutInflater li = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView   == null) {
        	
        	convertView = li.inflate(R.layout.each_item, null);
        	
        	viewHolder           = new ViewHolder();
        	viewHolder.textView  = (TextView) convertView.findViewById(R.id.icon_text);
        	viewHolder.imageView = (ImageView) convertView.findViewById(R.id.icon_image);
        	
        	convertView.setTag(viewHolder);
        	
        	
            
        } else {
        	ResolveInfo resolveInfo = MyAppList.get(position);
        	viewHolder = (ViewHolder) convertView.getTag();
        	viewHolder.imageView.setImageDrawable(resolveInfo.loadIcon(myPackageManager));
        	viewHolder.textView.setText(resolveInfo.loadLabel(myPackageManager));
           
        }
        return convertView;
 
    }

}